package edu.zju;

import java.util.concurrent.CountDownLatch;

public class BurnRunnable implements Runnable {
	private int usbnumber;
	private int nodeid;
	private String filename;
	private CountDownLatch latch;
	public int exitValue;
	
	public static String usbandvalue = "";
	public static int index = 0;

	public BurnRunnable(int nodeid, int usbnumber, String filename,CountDownLatch latch) { //NOTE: filename should be full path
		this.nodeid = nodeid;
		this.usbnumber = usbnumber;
		this.filename = filename;
		this.latch = latch;
	}
	
	public static void end(){
		usbandvalue = "";
		index = 0;
	}

	public void run() {
		String cmd = "tos-bsl --telosb -c /dev/ttyUSB" + this.usbnumber
				+ " -r -e -I -p " + this.filename;
		System.out.println("BurnRunnable: burnning " + this.usbnumber + "\n" + cmd + "\n");
		try {
			Process process = java.lang.Runtime.getRuntime().exec(cmd);
			this.exitValue = process.waitFor();
			usbandvalue += this.nodeid + "," + this.exitValue + ",";
			index++;
			latch.countDown();
		} catch (Exception ee) {
		}
	}
	
	
}
